package com.example.provider.service.impl;

import com.example.common.entity.Invite;
import com.example.common.entity.Message;
import com.example.provider.dao.BiddingDao;
import com.example.provider.dao.InviteDao;
import com.example.provider.dao.ScheduleDao;
import com.example.provider.service.InviteService;
import com.example.provider.utils.IdWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

@Service
public class InviteServiceImpl implements InviteService {
    @Autowired
    private InviteDao inviteDao;
    @Autowired
    private ScheduleDao scheduleDao;
    @Autowired
    private BiddingDao biddingDao;


    /**
     * 添加企业对工作室的邀请
     * @param demandId 需求id
     * @param workroomId 被邀请工作室id
     * @return
     */
    @Transactional
    public Message addInvitation(Long demandId, int workroomId, int companyId) {
        inviteDao.insert(new IdWorker(1,1,1).nextId(),
                demandId, workroomId, companyId);
        return new Message(200, "添加成功");
    }

    /**
     * 工作室查看公司发起的邀请
     * @param workroomId 工作室id
     * @return
     */
    public List<Invite> checkInvitation(int workroomId) {
        return inviteDao.searchByWorkroomId(workroomId);
    }

    /**
     * 公司查看工作室对邀请的回复
     * @param companyId 企业id
     * @return
     */
    public List<Invite> checkReply(int companyId) {
        return inviteDao.searchByCompanyId(companyId);
    }

    /**
     * 工作室回复邀请，若接受项目进入开工状态
     * @param map
     * @return
     */
    public Message replyInvitation(Map<String, Long> map) {
        int isAgree = Integer.parseInt(String.valueOf(map.get("isAgree")));
        inviteDao.updateById(map.get("id"), isAgree);
        if (isAgree == 1){
            int workroomId = Integer.parseInt(String.valueOf(map.get("workroomId")));
            long demandId = map.get("demandId");
            scheduleDao.insert(new IdWorker(1,1,1).nextId(),
                    demandId,
                    workroomId,
                    Integer.parseInt(String.valueOf(map.get("companyId"))));
            //删除对应需求的竞标，因为已经进入项目开发阶段了
            biddingDao.deleteByWIdAndGId(workroomId, demandId);
        }
        return new Message(200, "成功");
    }
}
